package com.health.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.health.dto.DoctorDTO;
import com.health.entity.Doctor;
import com.health.entity.Schedule;
import com.health.mapper.DoctorMapper;
import com.health.mapper.ScheduleMapper;
import com.health.result.PageResult;
import com.health.service.DoctorService;
import com.health.service.ScheduleService;
import com.health.vo.DoctorPageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DoctorServiceImpl implements DoctorService {
    @Autowired
    private ScheduleMapper scheduleMapper;
    @Autowired
    private DoctorMapper doctorMapper;

    @Override
    public List<DoctorPageVO> list(DoctorDTO doctorDTO) {
        PageHelper.startPage(doctorDTO.getPage(), doctorDTO.getPageSize());
        //根据科室id和日期去查排班表
        Page<Schedule> page = (Page<Schedule>) scheduleMapper.list(doctorDTO);
        List<Schedule> result = page.getResult();
        List<Long> depIds = result.stream().map(Schedule::getDeptId).collect(Collectors.toList());
        //根据depIds去批量查询医生表获得医生信息
        List<Doctor> doctors = doctorMapper.selectAll(depIds);
        Map<Long, Integer> map = result.stream().collect(Collectors.toMap(Schedule::getDoctorId, Schedule::getNumberAmount));
        List<DoctorPageVO> collect2 = doctors.stream().map(doctor -> {
            //找到每一个doctor表的数据 并赋值进去
            DoctorPageVO doctorPageVO = new DoctorPageVO();
            BeanUtils.copyProperties(doctor, doctorPageVO);
            //根据doctor id获取到他的号源数
            Integer stock = map.get(doctor.getId());
            if(stock == null){
                stock = 0;
            }
            doctorPageVO.setStock(stock);
            //号源数为0的时候说明排满了
            if(stock >0 ){
                doctorPageVO.setSellout(false);
            }else {
                doctorPageVO.setSellout(true);
            }
            return doctorPageVO;
        }).collect(Collectors.toList());
        return collect2;

    }
}
